//
//  GLTabBarController.m
//  GLTabBar
//
//  Created by 张国梁 on 15/11/2.
//  Copyright © 2015年 CoderGL. All rights reserved.
//

#import "GLTabBarController.h"
#import "GLTabBar.h"

#import "GLNavigationController.h"

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourViewController.h"


@interface GLTabBarController ()<GLTabBarDelegate>

@end

@implementation GLTabBarController

+ (void)initialize {
    
    // 设置UITabBarItem主题
    [self setupTabBarItemTheme];
    
    // 设置UITabBar主题
    [self setupTabBarTheme];
}

+ (void)setupTabBarItemTheme {
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:10], NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateSelected];
}

+ (void)setupTabBarTheme {
    
//    UITabBar *tabBar = [UITabBar appearance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加所有子控制器
    [self addAllViewControllers];
    
    // 创建自定义TabBar
    [self addCustomTabBar];
}
#pragma mark - 添加所有子控制器
- (void)addAllViewControllers {
    FirstViewController *firstVc = [FirstViewController new];
    [self addOneChildVc:firstVc title:@"首页" imageName:@"home_normal" selectedImageName:@"home_highlight"];
    
    SecondViewController *secondVc = [SecondViewController new];
    [self addOneChildVc:secondVc title:@"同城" imageName:@"mycity_normal" selectedImageName:@"mycity_highlight"];
    
    ThirdViewController *thirdVc = [ThirdViewController new];
    [self addOneChildVc:thirdVc title:@"消息" imageName:@"message_normal" selectedImageName:@"message_highlight"];
    
    FourViewController *fourVc = [FourViewController new];
    [self addOneChildVc:fourVc title:@"我的" imageName:@"account_normal" selectedImageName:@"account_highlight"];
}

#pragma mark - 自定义TabBar
- (void)addCustomTabBar {
    GLTabBar *tabBar = [GLTabBar new];
    tabBar.tabBarDelegate = self;
    [self setValue:tabBar forKeyPath:@"tabBar"];
}


// GLTabBarDelegate method - - - - -
- (void)tabBarDidClickCustomButton:(GLTabBar *)tabBar {
    NSLog(@"tabBarDidClickCustomButton");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"测试" message:@"测试提示" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"发布" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"一键转卖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - 添加一个子控制器
- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)seletedImageName {
    
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:seletedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:[[GLNavigationController alloc] initWithRootViewController:childVc]];
}

@end
